package edu.johnpank.libassistant.services

import edu.johnpank.libassistant.entity.User
import edu.johnpank.libassistant.repo.UserRepository
import edu.johnpank.libassistant.security.PersonDetails
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.security.core.userdetails.UsernameNotFoundException

import org.springframework.stereotype.Service
import java.util.*

@Service
class UserDetailServiceImpl (
    @Autowired
    val userRepository: UserRepository
): UserDetailsService {


    //UserDetailService impl
    override fun loadUserByUsername(username: String?): UserDetails {
        val person: Optional<User>? = username?.let { userRepository.findByLogin(it) }
        if (person!!.isEmpty)
            throw UsernameNotFoundException("!!User not found!!")

        return PersonDetails(person.get())
    }
}